package com.yanqu.road.server.cmd.beautyescort;

import com.yanqu.road.entity.beautyescort.EscortBeautyDispatch;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.pb.beautyescort.BeautyEscortProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.beautyescort.CrossBeautyEscortMgr;
import com.yanqu.road.server.manager.beautyescort.CrossEscortPlayerWrap;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;

import java.util.List;

@Cmd(code = Protocol.C_BEAUTY_ESCORT_DART_ACCEPT_DISPATCH_BEAUTY, desc = "接镖+美女派遣")
public class CrossBeautyEscortAcceptDispatchDartCmd extends CrossCommand {

    private int client;
    private int type;
    private List<EscortBeautyDispatch> dispatchList;
    private long playerId;
    private List<Integer> buyList;
    private String buyBeautyConsume;
    private boolean autoAccelerate;
    private int needAccelerateCount;
    private int accelerateTime;
    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        BeautyEscortProto.CrossBeautyEscortAcceptDispatchReqMsg reqMsg = BeautyEscortProto.CrossBeautyEscortAcceptDispatchReqMsg.parseFrom(packet.getBytes());
        client = reqMsg.getClient();
        type = reqMsg.getTypeId();
        playerId = packet.getPlayerId();
        buyList = reqMsg.getBuyBeautyList();
        dispatchList = CrossBeautyEscortMgr.parseBeautyDispatch(reqMsg.getBeautyList());
        buyBeautyConsume = reqMsg.getBuyBeautyConsume();
        autoAccelerate = reqMsg.getAutoAccelerate();
        needAccelerateCount = reqMsg.getNeedAccelerateCount();
        accelerateTime = reqMsg.getAccelerateTime();
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        CrossEscortPlayerWrap escortPlayer = CrossBeautyEscortMgr.getEscortPlayer(serverId, packet.getPlayerId());
        if(escortPlayer != null){
            ret = escortPlayer.acceptAndDispatchDart(client, type, dispatchList, buyList, buyBeautyConsume, autoAccelerate,
                    needAccelerateCount, accelerateTime);
        }else{
            ret = GameErrorCode.E_BEAUTY_ESCORT_USER_NOT_FOUND;
        }
    }

    @Override
    public void sendData() {
        if(ret != 0){
            BeautyEscortProto.CrossBeautyEscortAcceptDispatchRespMsg.Builder respMsg = BeautyEscortProto.CrossBeautyEscortAcceptDispatchRespMsg.newBuilder();
            respMsg.setRet(ret);
            respMsg.setClient(client);
            respMsg.setTypeId(type);
            respMsg.addAllBuyBeauty(buyList);
            respMsg.addAllBeauty(CrossBeautyEscortMgr.parseBeautyDispatchBuilderList(dispatchList));
            respMsg.setBuyBeautyConsume(buyBeautyConsume);
            respMsg.setAutoAccelerate(autoAccelerate);
            respMsg.setNeedAccelerateCount(needAccelerateCount);
            MessageHelper.sendPacket(serverId, playerId, YanQuMessageUtils.buildMessage(Protocol.S_BEAUTY_ESCORT_ACCEPT_DISPATCH_BEAUTY_FROM_CROSS, respMsg));
        }
    }
}
